package OT.shoufouni.mainGUI;

import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class myContainer extends SQLiteOpenHelper{
	private static final int DATABASE_VERSION = 2;
    private static final String TABLE_NAME = "mfriendsTableName";
    private static final String DB_NAME = "mfriendsDBName";
    private static final String FRIENDS_TABLE_CREATE =
    	"CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
    			" (Name VARCHAR, Tel VARCHAR, EmailId VARCHAR);";

	public myContainer(Context context){
		super(context, DB_NAME , null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(FRIENDS_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	public void killDB(){
		getWritableDatabase().execSQL("DELETE FROM " + TABLE_NAME);
		getWritableDatabase().close();
	}
	
	public void addNewFriendToDB(String name, String tel, String emailId){
    	if ( (!(name.equals(""))) && (!(tel.equals(""))) && (!(emailId.equals(""))) ){	
	    	try {
	    		getWritableDatabase().execSQL("INSERT INTO " + TABLE_NAME +
	    			" Values ('" + name +"', '" + tel +"', '" + emailId +"');");
	        }
	        catch (SQLiteException se ) {
	        	Log.e(getClass().getSimpleName(), "Could not create records");
	        }
    	}
    	else
    		return;
    }
	
	public void addFriendsArrayToDB(ArrayList<Map<String, String>> newFriends){
		SQLiteDatabase writableDB = getWritableDatabase();
		String name, tel, emailId;
		for (int i=0;i<newFriends.size();i++){
			name = newFriends.get(i).get("NAME");
			tel = newFriends.get(i).get("TEL");
			emailId = newFriends.get(i).get("ADDRESS");
			writableDB.execSQL("INSERT INTO " + TABLE_NAME +
	    			" Values ('" + name +"', '" + tel +"', '" + emailId +"');");
		}
		writableDB.close();
	}
	
    public ArrayList<String> findFriendInDB(String emailId){
    	ArrayList<String> friends = new ArrayList<String>();
    	SQLiteDatabase readableDB = null;
    	readableDB = getReadableDatabase();
     	Cursor c = readableDB.rawQuery("SELECT Name, Tel, EmailId  FROM "	+
    			TABLE_NAME + " where EmailId == " + emailId , null);
    	//load the values from the database to arrayList
     	if (c != null ) {
    		if  (c.moveToFirst()) {
    			do {
    				String val = c.getString(c.getColumnIndex("Name")) +
    				", tel - " + c.getString(c.getColumnIndex("Tel"));
    				friends.add(val);
    			}while (c.moveToNext());
    		} 
    	}
     	readableDB.close();
     	return friends;
    }
    
    // get cursor to the friends db
    public Cursor getFriendsReadableCursor(){
    	SQLiteDatabase readableDB = null;
    	readableDB = getReadableDatabase();
    	Cursor c = readableDB.rawQuery("SELECT Name, Tel, EmailId  FROM "	+
    			TABLE_NAME , null);
    	return c;
    }
    
}
	    

